# -*- coding: utf-8 -*-
import happybase

# 实现连接类方法和通用表操作方法
class HBaseConnect:
    def __init__(self):
        """
        建立与thrift server端的连接
        """
        self.connection = happybase.Connection(host="192.168.226.134", port=9090)

    def getTable(self, table_name: str):  # Return Happybase Table
        return self.connection.table(table_name)

    def start(self):  # Start To Connect
        self.connection.open()

    def stop(self):  # Stop To Connect
        self.connection.close()

    # 删除表
    def deleteTable(self, tableName):
        self.connection.disable_table(tableName)
        self.connection.delete_table(tableName)

    def printTables(self):
        return self.connection.tables()

    def createTable(self, tableName, families):
        self.connection.create_table(tableName, families)

    def putTable(self, tableName, rowKey, data):
        table = self.connection.table(tableName)
        table.put(rowKey, data)


if __name__ == '__main__':
    hbase = HBaseConnect()
    hbase.start()
    hbase.createTable("hot_search_keywords", {"info": dict()})
    print(hbase.printTables())
    hbase.stop()

